package enn;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URI;

/**
 * Created by Yu on 2017/11/6.
 */
@WebServlet("/gasServlet")
public class gasServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");         //返回页面防止出现中文乱码

//        Enumeration enu=request.getParameterNames();
//        while(enu.hasMoreElements()){
//            String paraName=(String)enu.nextElement();
//            System.out.println(paraName+": "+request.getParameter(paraName));
//        }
        //String json=request.getParameter("gasType");
       // String imgContent=request.getParameter("imgContent");

        //File file=new File("D:\\reqParam.txt");
        //String imgContent= FileUtils.readFileToString(file,"UTF-8");

//        StringBuffer sb = new StringBuffer ();
//        InputStream is = request.getInputStream();
//        System.out.println(is.available());
//        BufferedReader br=new BufferedReader(new InputStreamReader(is));
//        BufferedInputStream bis = new BufferedInputStream(is);
//        String s;
//        while ((s=br.readLine()) !=null){
//            sb.append(s);
//        }
//        out.write(sb.toString());

        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));//post方式传递读取字符流

        String jsonStr = null;

        StringBuilder result = new StringBuilder();

        try {

            while ((jsonStr = reader.readLine()) != null) {

                result.append(jsonStr);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

        reader.close();// 关闭输入流

        JSONObject jsonObject=new JSONObject().fromObject(result.toString());
        RecogVO recogVO = new RecogVO();
        //旧
//        byte[] imgArr = Base64.decodeBase64(jsonObject.getString("imgContent"));
//        String imgname=System.currentTimeMillis()+".jpg";
//        String path=request.getSession().getServletContext().getRealPath("/")+"pic";
//        File f = new File(path);
//        if (!f.exists()) {
//            f.mkdirs();
//        }
//        if (f.list().length>200){
//        aaautil.delAllFile(path);
//        }
//        aaautil.byte2image(imgArr,path+File.separator+imgname);
        recogVO.setJson(jsonObject.getString("json"));
        //recogVO.setImgContent("/pic/"+imgname);
        recogVO.setImgContent(jsonObject.getString("url"));
       // aaautil.byte2File(imgarr,request.getSession().getServletContext().getRealPath("/"),"a.jpg");
        try {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer(); // 获取WebSocket连接器，其中具体实现可以参照websocket-api.jar的源码,Class.forName("org.apache.tomcat.websocket.WsWebSocketContainer");
            //container.setDefaultMaxTextMessageBufferSize(999999999);
            String uri = "ws://localhost:8080/getmessage";
                         Session session = container.connectToServer(Client.class, new URI(uri)); // 连接会话
                         System.out.println(recogVO.toString());
                         session.getBasicRemote().sendText(recogVO.toString()); // 发送文本消息
                         //session.getBasicRemote().sendBinary(ByteBuffer.wrap(result.toString().getBytes()));
                         session.close();
                     } catch (Exception e) {
                         e.printStackTrace();
                     }

        PrintWriter out=response.getWriter();
        out.write(1);
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

